#include "StdAfx.h"
#include "ServerProc.h"
#include <ctime>



ServerProc::ServerProc(void)
{
}


ServerProc::~ServerProc(void)
{
}

int ServerProc::nickNameProc(char nick[],char** nickname,int n)
{
	int len = strlen(nick);
	if(len==0)
		return -1;
	for(int i = 0; i < len;i++)
	{
		char temp = nick[i];
		if((temp <'0')||(temp >'9'&&temp <'A')||(temp >'Z'&&temp <'a')||(temp > 'z'))
		{
			return 0;
		}
	}
	for(int i = 0 ; i <= n;i++)
	{
		if(strcmp(nick,nickname[i]) == 0)
		{
			return -2;
		}
	}
	return 1;
}
BOOL ServerProc::loadQuestion(char *buffer, char *fileName, int line)
{

	fstream file;
	file.open(fileName, ios::in);
	if(!file) return 0;
	char *str = new char[1001];
	// cac lenh thuc thi
	for( int i = 0 ; i < line - 1; i++)
	{
		file.getline(str,1000);
		
	}
	file.getline(str,1000);
	
	strcpy(buffer,str);
	file.close();
	delete str;
	return 1;
}
BOOL ServerProc::randomQuestion(int *arrQuestion ,int numQR, int numQA) // return arr of question to arrQuestion
// num: so luong cau hoi co trong thu vien cau hoi
{
	int *arrRan = new int[numQR];
	time_t t;
	time(&t);
	srand((unsigned int)t);
	for(int i = 0 ; i < numQR ; i++)
	{
		int temp = arrRan[i] = rand()%numQA+1;
		if(buffer.CountData(temp)==0)
			buffer.AddHead(temp);
		else 
			i--;
	}
	return TRUE;
}

// viet them 1 ham update de refresh list nick va lay ra so nguoi` hien tai trong list
int ServerProc::countPlayer(char** nickname,int n)
{
	int count = 0;
	for (int i = 0 ; i < n ; i++)
	{
		if(nickname[i]!=NULL)
			count++;
	}
	return count;
}


int ServerProc::getLastPlayer(char** nickname, int n)
{
	
	for (int i = n-1 ; i >=0 ; i--)
		if(nickname[i]!=NULL)
			return i;
	
}
int ServerProc::getFirstPlayer(char** nickname, int n)
{
	for (int i = 0 ; i < n ; i++)
		if(nickname[i]!=NULL)
			return i;
}